<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination
date_default_timezone_set("PRC");
require_once '../../include/image.func.php';

$targetFolder = '/attached/' . date ( 'Ymd', time () ); // Relative to the root


$width = $_POST ['width'];
$height = $_POST ['height'];

$verifyToken = md5 ( 'unique_salt' . $_POST ['timestamp'] );

if (! empty ( $_FILES ) && $_POST ['token'] == $verifyToken) {
	$tempFile = $_FILES ['Filedata'] ['tmp_name'];
	$targetPath = $_SERVER ['DOCUMENT_ROOT'] . $targetFolder;
	if (! is_file ( $targetPath ))
		@mkdir ( $targetPath );
		// Validate the file type
	$fileTypes = array ('jpg', 'jpeg', 'gif', 'png' ); // File extensions
	$fileParts = pathinfo ( $_FILES ['Filedata'] ['name'] );
	
	$new_file_name = time () . '.' . $fileParts ['extension'];
	$thumb_file_name = 'thumb_' . $new_file_name;
	$targetFile = rtrim ( $targetPath, '/' ) . '/' . $new_file_name;
	$thumbtargetFile = rtrim ( $targetPath, '/' ) . '/' . $thumb_file_name;
	
	if (in_array ( strtolower ( $fileParts ['extension'] ), $fileTypes )) {
		move_uploaded_file ( $tempFile, $targetFile );
		if ($width > 0) {
			move_uploaded_file ( $tempFile, $targetFile );
			make_thumb ( $targetFile, $thumbtargetFile, $width, $height, 80 );
			echo $targetFolder . '/' . $thumb_file_name;
		} else
			echo $targetFolder . '/' . $new_file_name;
	} else {
		echo 'Invalid file type.';
	}
}
?>